package com.k8sops.backend.controller;

import com.k8sops.backend.service.KubernetesService;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/kubernetes")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class KubernetesController {

    private final KubernetesService kubernetesService;

    @Autowired
    public KubernetesController(KubernetesService kubernetesService) {
        this.kubernetesService = kubernetesService;
    }

    @GetMapping("/connection")
    public ResponseEntity<Map<String, Boolean>> checkConnection() {
        boolean isConnected = kubernetesService.checkConnection();
        Map<String, Boolean> response = new HashMap<>();
        response.put("connected", isConnected);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/namespaces")
    public ResponseEntity<List<String>> getNamespaces() {
        try {
            return ResponseEntity.ok(kubernetesService.getNamespaces());
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }

    @GetMapping("/namespaces/{namespace}/pods")
    public ResponseEntity<List<V1Pod>> getPods(@PathVariable String namespace) {
        try {
            return ResponseEntity.ok(kubernetesService.getPods(namespace));
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }

    @GetMapping("/namespaces/{namespace}/deployments")
    public ResponseEntity<List<V1Deployment>> getDeployments(@PathVariable String namespace) {
        try {
            return ResponseEntity.ok(kubernetesService.getDeployments(namespace));
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }

    @GetMapping("/namespaces/{namespace}/services")
    public ResponseEntity<List<V1Service>> getServices(@PathVariable String namespace) {
        try {
            return ResponseEntity.ok(kubernetesService.getServices(namespace));
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }

    @GetMapping("/nodes")
    public ResponseEntity<List<V1Node>> getNodes() {
        try {
            return ResponseEntity.ok(kubernetesService.getNodes());
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }

    @GetMapping("/namespaces/{namespace}/pods/{podName}")
    public ResponseEntity<V1Pod> getPodDetails(
            @PathVariable String namespace,
            @PathVariable String podName) {
        try {
            return ResponseEntity.ok(kubernetesService.getPodDetails(namespace, podName));
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }

    @GetMapping("/namespaces/{namespace}/pods/{podName}/logs")
    public ResponseEntity<String> getPodLogs(
            @PathVariable String namespace,
            @PathVariable String podName,
            @RequestParam(required = false) String containerName) {
        try {
            return ResponseEntity.ok(kubernetesService.getPodLogs(namespace, podName, containerName));
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }

    @DeleteMapping("/namespaces/{namespace}/pods/{podName}")
    public ResponseEntity<Object> deletePod(
            @PathVariable String namespace,
            @PathVariable String podName) {
        try {
            return ResponseEntity.ok(kubernetesService.deletePod(namespace, podName));
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }
    
    /**
     * 获取所有Helm Releases
     */
    @GetMapping("/releases")
    public ResponseEntity<Object> getAllReleases() {
        try {
            return ResponseEntity.ok(kubernetesService.getAllReleases());
        } catch (ApiException e) {
            return ResponseEntity.status(e.getCode()).build();
        }
    }
}
